import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ExampleTest {

	public static void main(String[] args) {
		File file0 = SimpleTest.getFileToOpen(args, 0, "input1.tsv.real", "Choose the real file");
		BufferedReader brReal;
		if (file0 == null) {
			return;
		} else {
			try {
				brReal = new BufferedReader(new FileReader(file0));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}
		}

		File file1 = SimpleTest.getFileToOpen(args, 1, "input1.tsv.fake", "Choose the fake file");
		BufferedReader brFake;
		if (file1 == null) {
			return;
		} else {
			try {
				brFake = new BufferedReader(new FileReader(file1));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}
		}

		int[] fakeValues = new int[159000];
		int[] realValues = new int[46000];
		String[] inputs;
		String line;
		MinorityVariants calculo = new MinorityVariants();
		int i = 0;
		int realValuesQtty = 0;
		int fakeValuesQtty = 0;

		// double m1 = 0, m2 = 0, m3 = 0;

		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(new File(file0.getAbsolutePath() + ".res")));
			while ((line = brReal.readLine()) != null) {
				inputs = line.split("\t");
				if (inputs.length > 0) {
					realValues[i] = calculo.calculateConfidence(inputs[2] + " " + inputs[3] + " " + inputs[4]);
					bw.write(line + "\t" + realValues[i]);
					bw.newLine();
					i++;
					// m1 += Double.parseDouble(inputs[2]);
					// m2 += Double.parseDouble(inputs[3]);
					// m3 += Double.parseDouble(inputs[4]);
				}
			}
			realValuesQtty = i;

			// System.out.println("Médias real:");
			// System.out.println(m1 / i);
			// System.out.println(m2 / i);
			// System.out.println(m3 / i);
			//
			// m1 = 0;
			// m2 = 0;
			// m3 = 0;

			i = 0;
			bw.flush();

			bw = new BufferedWriter(new FileWriter(new File(file1.getAbsolutePath() + ".res")));
			while ((line = brFake.readLine()) != null) {
				inputs = line.split("\t");
				if (inputs.length > 0) {
					fakeValues[i] = calculo.calculateConfidence(inputs[2] + " " + inputs[3] + " " + inputs[4]);
					bw.write(line + "\t" + fakeValues[i]);
					bw.newLine();
					i++;
					// m1 += Double.parseDouble(inputs[2]);
					// m2 += Double.parseDouble(inputs[3]);
					// m3 += Double.parseDouble(inputs[4]);
				}
			}
			fakeValuesQtty = i;

			// System.out.println("Médias fake:");
			// System.out.println(m1 / i);
			// System.out.println(m2 / i);
			// System.out.println(m3 / i);

			double score = 0;
			for (int r1 : realValues) {
				for (int r2 : fakeValues) {
					score += (r1 > r2 ? 1 : r1 == r2 ? 0.5 : 0);
				}
			}
			score = score / fakeValuesQtty;
			score = score / realValuesQtty;
			// score /= (fakeValuesQtty * realValuesQtty);
			score *= 1000000;

			System.out.println(score);

			bw.flush();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
